package com.szrt.cookie;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.szrt.cookie.utils.CookieUtil;

public class CookieServlet extends HttpServlet {

	private static final long serialVersionUID = -3805640999174935654L;
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		String time = new Date().toString();
		Cookie c1 = null;
		//拿lastTime的cookie  如果为null 第1次来
		if(null ==CookieUtil.getCookie(request, "lastTime")){
			//说明第1次过来
			c1 = new Cookie("lastTime",time);
		}else{
			//说明第1次过来
			c1 = new Cookie("lastTime",CookieUtil.getCookie(request, "currentTime").getValue());
		}
		
		Cookie c2 = new Cookie("currentTime",time);
		response.addCookie(c1);
		response.addCookie(c2);
		
		//重定向
		response.sendRedirect(request.getContextPath()+"/cookieSuccess.jsp");
		
	}

}
